package com.ultrasoft.farmasoft.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Credititempharmacy;

@Repository(value = "creditItemPharmacyDao")
@Transactional(readOnly = false)
public class CreditItemPharmacyDaoImpl implements CreditItemPharmacyDao {

	private EntityManager em = null;
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}
	public List<Credititempharmacy> getListCreditItemPharmacy() {
		TypedQuery<Credititempharmacy> q = em.createNamedQuery("Credititempharmacy.findAll",Credititempharmacy.class);
    	return q.getResultList();
	}

	public List<Credititempharmacy> getListCreditItemPharmacyByAccountsPayableId(Integer accountsPayableId) {
		TypedQuery<Credititempharmacy> q = em.createNamedQuery("Credititempharmacy.findByAccountsPayableId",Credititempharmacy.class);
    	q.setParameter("accountsPayableId",accountsPayableId);
    	return q.getResultList();
	}

	public List<Credititempharmacy> getListCreditItemPharmacyByPackageId(Integer packageId) {
		TypedQuery<Credititempharmacy> q = em.createNamedQuery("Credititempharmacy.findByPackageId",Credititempharmacy.class);
    	q.setParameter("packageId",packageId);
    	return q.getResultList();
	}

	public List<Credititempharmacy> getListCreditItemPharmacyByItemId(Integer itemId) {
		TypedQuery<Credititempharmacy> q = em.createNamedQuery("Credititempharmacy.findByItemId",Credititempharmacy.class);
    	q.setParameter("itemId",itemId);
    	return q.getResultList();
	}

	public List<Credititempharmacy> getListCreditItemPharmacyByPackageIdByItemId(Integer packageId, Integer itemId) {
		TypedQuery<Credititempharmacy> q = em.createNamedQuery("Credititempharmacy.findByPackageIdByItemId",Credititempharmacy.class);
    	q.setParameter("packageId",packageId);
    	q.setParameter("itemId",itemId);
    	return q.getResultList();
	}

	public Credititempharmacy findCreditItemPharmacyByAccountsPayableIdByPackageIdByItemId(Integer accountsPayableId, Integer packageId, Integer itemId) {
		Credititempharmacy res = null;
    	Query  q = em.createNamedQuery("Credititempharmacy.findByAccountsPayableIdByPackageIdByItemId");
    	q.setParameter("accountsPayableId",accountsPayableId);
    	q.setParameter("packageId",packageId);
    	q.setParameter("itemId",itemId);
    	if(q.getResultList().size()==1)
    		res=(Credititempharmacy)q.getSingleResult();
    	return res;
	}
	
	public void remove(Credititempharmacy creditItemPharmacy) {
		Query q = em.createNamedQuery("Credititempharmacy.deleteByAccountsPayableIdByPackageIdByItemId");
		q.setParameter("accountsPayableId",creditItemPharmacy.getCredititempharmacyPK().getAccountsPayableId());
    	q.setParameter("packageId",creditItemPharmacy.getCredititempharmacyPK().getPackageId());
    	q.setParameter("itemId",creditItemPharmacy.getCredititempharmacyPK().getItemId());
		q.executeUpdate();
	}

	public void update(Credititempharmacy creditItemPharmacy) {
		em.merge(creditItemPharmacy);
	}

	public void add(Credititempharmacy creditItemPharmacy) {
		em.persist(creditItemPharmacy);
	}

}
